package net.wexoo.onlinedivelog.reusable.behaviour;

import org.apache.wicket.Component;
import org.apache.wicket.ajax.AjaxRequestTarget;
import org.apache.wicket.ajax.form.AjaxFormComponentUpdatingBehavior;

public class AjaxFormComponentUpdater extends AjaxFormComponentUpdatingBehavior {
   private static final long serialVersionUID = 1L;

   private final Component componentToUpdate;

   public AjaxFormComponentUpdater(Component componentToUpdate) {
      this("onblur", componentToUpdate);
   }

   public AjaxFormComponentUpdater(String event, Component componentToUpdate) {
      super(event);

      this.componentToUpdate = componentToUpdate;
   }

   @Override
   protected void onError(AjaxRequestTarget target, RuntimeException e) {
      if (componentToUpdate != null)
         target.add(componentToUpdate);
   }

   @Override
   protected void onUpdate(AjaxRequestTarget target) {
      if (componentToUpdate != null)
         target.add(componentToUpdate);
   }
}